package cn.iocoder.yudao.module.scenic.controller.admin.spot;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.scenic.controller.admin.spot.vo.SpotPageByLocationReqVO;
import cn.iocoder.yudao.module.scenic.controller.admin.spot.vo.SpotPageReqVO;
import cn.iocoder.yudao.module.scenic.controller.admin.spot.vo.SpotRespVO;
import cn.iocoder.yudao.module.scenic.controller.admin.spot.vo.SpotSaveReqVO;
import cn.iocoder.yudao.module.scenic.dal.dataobject.spot.SpotDO;
import cn.iocoder.yudao.module.scenic.service.spot.SpotService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 实践点")
@RestController
@RequestMapping("/scenic/spot")
@Validated
public class SpotController {

    @Resource
    private SpotService spotService;

    @PostMapping("/create")
    @Operation(summary = "创建实践点")
    @PreAuthorize("@ss.hasPermission('scenic:spot:create')")
    public CommonResult<Long> createSpot(@Valid @RequestBody SpotSaveReqVO createReqVO) {
        return success(spotService.createSpot(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新实践点")
    @PreAuthorize("@ss.hasPermission('scenic:spot:update')")
    public CommonResult<Boolean> updateSpot(@Valid @RequestBody SpotSaveReqVO updateReqVO) {
        spotService.updateSpot(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除实践点")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('scenic:spot:delete')")
    public CommonResult<Boolean> deleteSpot(@RequestParam("id") Long id) {
        spotService.deleteSpot(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得实践点")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('scenic:spot:query')")
    public CommonResult<SpotRespVO> getSpot(@RequestParam("id") Long id) {
        SpotDO spot = spotService.getSpot(id);
        return success(BeanUtils.toBean(spot, SpotRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得实践点分页")
    @PreAuthorize("@ss.hasPermission('scenic:spot:query')")
    public CommonResult<PageResult<SpotRespVO>> getSpotPage(@Valid SpotPageReqVO pageReqVO) {
        PageResult<SpotDO> pageResult = spotService.getSpotPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SpotRespVO.class));
    }

    @PostMapping("/spot_page_by_location")
    @Operation(summary = "根据省市区获得实践点分页")
    @PreAuthorize("@ss.hasPermission('scenic:spot:query')")
    public CommonResult<PageResult<SpotRespVO>> getSpotPageByLocation(@RequestBody @Valid SpotPageByLocationReqVO pageReqVO) {
        PageResult<SpotRespVO> pageResult = spotService.getSpotPageByLocation(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出实践点 Excel")
    @PreAuthorize("@ss.hasPermission('scenic:spot:export')")
    @OperateLog(type = EXPORT)
    public void exportSpotExcel(@Valid SpotPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SpotDO> list = spotService.getSpotPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "实践点.xls", "数据", SpotRespVO.class,
                        BeanUtils.toBean(list, SpotRespVO.class));
    }

}